package root.electronclass.electronclass.other

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import root.electronclass.electronclass.common.base.ViewBindingActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.ActivityFullImgBinding
import root.electronclass.electronclass.mvp.entity.FullScreenImg
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoaderInterface

class FullImgActivity : ViewBindingActivity<ActivityFullImgBinding>() {
    override fun init() {
        var list: List<FullScreenImg> = intent.getSerializableExtra("fullScreenImgData") as List<FullScreenImg>
        binding.banner.setImageLoader(GlideImageLoader())
        binding.banner.setImages(list)
        binding.banner.isAutoPlay(true)
        binding.banner.setDelayTime(3000)
        binding.banner.setIndicatorGravity(BannerConfig.CENTER)
        binding.banner.start()
        binding.banner.setOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                binding.title.text = list[position].name
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        binding.banner.setOnBannerListener {
            setResult(Activity.RESULT_OK)
            finish()
        }
    }
    class GlideImageLoader : ImageLoaderInterface<View> {
        override fun displayImage(context: Context, path: Any, imageView: View) { //            TextView title = imageView.findViewById(root.electronclass.home.R.id.title);
            val image: ImageView = imageView.findViewById(R.id.image)
            val data: FullScreenImg = path as FullScreenImg
            if (StringUtils.isEmpty(data.url)) {
                Glide.with(context).load(context.getDrawable(R.mipmap.bg1)).into(image)
            } else {
                Glide.with(context).load( data.url).into(image)
            }
        }

        override fun createImageView(context: Context): View {
            return LayoutInflater.from(context).inflate(R.layout.full_screen_item, null)
        }
    }
    override fun onStop() {
        super.onStop()
        binding.banner.stopAutoPlay()
    }
}


